import { Component, OnInit, DoCheck } from '@angular/core';

declare var form: any;
declare var base: any;

@Component({
  templateUrl: './map.attribute.html'
})
export class MapAttributeComponent implements OnInit, DoCheck {
  fs: any = null;
  layerType: any = null;
  dicTypeDic: any = null;
  meDic: any = null;
  ffs: any = null;
  terminalType: any = null;

  filters: any[] = [];
  shows: any[] = [];
  titles: any[] = [];
  contents: any[] = [];

  oldId = '';
  oldMid = '';
  oldDatas = [];

  constructor() {
    // 注册表单
    this.fs = new form.FormTreeService('gisattributeconfig', {
      orderObject: { order: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 1000
    });
    this.fs.loadData(true);

    // 初始化字典
    this.layerType = new form.DictionaryService('d_souce_server_type');
    this.dicTypeDic = new form.DictionaryService('d_dictype');
    this.terminalType = new form.DictionaryService('d_terminal');
    this.meDic = new form.DictionaryService('f_metadata_directory', this.filter);
    this.meDic.loadAll();

    // 文件服务
    this.ffs = new form.FileService();
  }


  filter(data) {
    if (data) {
      const ri = [];
      for (let i = data.length - 1; i >= 0; i--) {
        const e = data[i];
        if (e.info && e.info === '2') {
          ri.push(i);
        }
      }
      ri.forEach((j) => {
        data.splice(j, 1);
      });
    }
    return data;
  }


  ngDoCheck(): void {
    const er = this.fs.er;
    if (!er) {
      return;
    }

    const mid = er.medname;
    // 无元数据时，清空
    if (!mid || mid === '') {
      this.filters = [];
      this.shows = [];
      this.contents = [];
      this.titles = [];
      return;
    }

    // 元数据未变化时，不重新加载元数据
    if (mid === this.oldMid) {
      // 如果ID值不一样，说明换了数据，重新绑定
      if (er.id !== this.oldId) {
        this.oldId = er.id;
        this.bind(er);
      }
      return;
    }

    // 重新加载数据，并绑定
    this.oldMid = mid;
    this.oldId = er.id;
    form.FormDataService.getMetadata(mid,
      (data) => {
        this.oldDatas = data;
        this.bind(er);
      }, (error) => {
      }
    );
  }

  bind(er) {
    if (!this.oldDatas) {
      this.filters = [];
      this.shows = [];
      this.titles = [];
      this.contents = [];
      return;
    }
    const ff = [];
    const sf = [];
    const tf = [];
    const cf = [];
    this.oldDatas.forEach(item => {
      const r1: any = {};
      base.BaseService.copy(item, r1, true);
      r1.title = item.aliasName + '(' + item.name + ')';
      if (er.filterField) {
        er.filterField.forEach(e => {
          if (r1.id === e.fieldid) {
            r1.direction = 'right';
            r1.isSelected = true;
          }
        });
      }

      ff.push(r1);

      const r2: any = {};
      base.BaseService.copy(item, r2, true);
      r2.title = item.aliasName + '(' + item.name + ')';
      if (er.showField) {
        er.showField.forEach(e => {
          if (r2.id === e.fieldid) {
            r2.direction = 'right';
            r2.isSelected = true;
          }
        });
      }
      sf.push(r2);

      const r3: any = {};
      base.BaseService.copy(item, r3, true);
      r3.title = item.aliasName + '(' + item.name + ')';
      if (er.titleField) {
        er.titleField.forEach(e => {
          if (r3.id === e.fieldid) {
            r3.direction = 'right';
            r3.isSelected = true;
          }
        });
      }

      tf.push(r3);

      const r4: any = {};
      base.BaseService.copy(item, r4, true);
      r4.title = item.aliasName + '(' + item.name + ')';
      if (er.contentField) {
        er.contentField.forEach(e => {
          if (r4.id === e.fieldid) {
            r4.direction = 'right';
            r4.isSelected = true;
          }
        });
      }

      cf.push(r4);

    });
    this.filters = ff;
    this.shows = sf;
    this.titles = tf;
    this.contents = cf;
  }

  change(ret): void {
    if (ret.from === 'left') {
      ret.list.forEach(e => {
        e.isSelected = true;
      });
    } else {
      ret.list.forEach(e => {
        e.isSelected = false;
      });
    }
  }

  save() {
    const fo = this.fs;
    if (fo.isSaveing) {
      return false;
    }
    const row = fo.er;
    if (row.dicType === '2') {
      row.leaf = true;
    } else if (row.dicType === '1') {
      row.leaf = false;
    }

    row.filterField = [];
    this.filters.forEach((e) => {
      if (e.isSelected === true) {
        row.filterField.push({
          id: base.BaseService.getGUID(),
          fieldid: e.id, name: e.name, aliasName: e.aliasName, dataType: e.dataType,
          order: e.order, dictionaryName: e.dictionaryName, group: e.group, dictionaryDataPid: e.dictionaryDataPid
        });
      }
    });

    row.titleField = [];
    this.titles.forEach((e) => {
      if (e.isSelected === true) {
        row.titleField.push({
          id: base.BaseService.getGUID(),
          fieldid: e.id, name: e.name, aliasName: e.aliasName, dataType: e.dataType,
          order: e.order, dictionaryName: e.dictionaryName
        });
      }
    });

    row.contentField = [];
    this.contents.forEach((e) => {
      if (e.isSelected === true) {
        row.contentField.push({
          id: base.BaseService.getGUID(),
          fieldid: e.id, name: e.name, aliasName: e.aliasName, dataType: e.dataType,
          order: e.order, dictionaryName: e.dictionaryName
        });
      }
    });

    row.showField = [];
    this.shows.forEach((e) => {
      if (e.isSelected === true) {
        row.showField.push({
          id: base.BaseService.getGUID(),
          fieldid: e.id, name: e.name, aliasName: e.aliasName, dataType: e.dataType,
          order: e.order, dictionaryName: e.dictionaryName
        });
      }
    });
    this.fs.save(row);
  }

  ngOnInit(): void {
  }
}
